<?php

/**
 *   OPENSHOP 管理中心期刊页
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: catalog_send.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');

if(!isset($_REQUEST['act']) || empty($_REQUEST['act'])){
	$_REQUEST['act'] = 'send';
}
if($_REQUEST['act'] == 'send'){
	
	admin_priv();
	
	$smarty->assign('form_act', 'setting_user');

    assign_query_info();
    
    $smarty->display('catalog_send.tpl');	
    
}elseif($_REQUEST['act']=='setting_user'){
	
	admin_priv('send_catalog');

    $smarty->assign('ur_here',      '目录期刊派发');
    $smarty->assign('full_page',    1);
    if($_GET['user_type'] == 0){
    	$list = get_rosterlist();    	
    	$smarty->assign('setting_user_list', $list['roster']);
    }elseif($_GET['user_type'] == 1){
    	$list = get_userlist();
    	$smarty->assign('setting_user_list', $list['user_list']);
    }
    
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
	$smarty->assign('form_act', 'send_catalog');

    assign_query_info();
    
    $smarty->display('setting_user.tpl');	
}elseif($_REQUEST['act']=='query'){    
   if($_GET['user_type'] == 0){
    	$list = get_rosterlist();    	
    	$smarty->assign('setting_user_list', $list['roster']);
    }elseif($_GET['user_type'] == 1){
    	$list = get_userlist();
    	$smarty->assign('setting_user_list', $list['user_list']);
    }

    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);
    
    make_json_result($smarty->fetch('setting_user.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));	
}elseif($_REQUEST['act']=='send_catalog'){   
	admin_priv('send_catalog');
	
	$catalog_ids = array();
	$catalog_ids = explode(',',$_POST['catalog_ids']);
	
	$user_list = array();
	$user_list = $_POST['checkboxes'];
	
	$user_type = intval($_POST['user_type']);
	
	foreach ($catalog_ids as $catalog_id){
		foreach ($user_list as $uu){
			$db->query("INSERT INTO ".$os->table('catalog_send')."(catalog_id,user_type,user_id,send_status,operate_time) ".
			           " VALUES('$catalog_id', '$user_type', '$uu',1,".gmtime().")");			
		}
	}
    $links = array(
        array('href' => 'catalog_send.php?act=send', 'text' => '继续目录期刊派发'),
        array('href' => 'catalog.php?act=list', 'text' => '返回目录期刊列表')
    );
    sys_msg('目录期刊派发成功', 0, $links);
	
}elseif($_REQUEST['act'] == 'get_catalog_list'){

	check_authz_json('send_catalog');
	
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $filters = $json->decode($_GET['JSON']);
   
    /* 取得数据 */
    $sql = "SELECT catalog_name,catalog_sn,catalog_id FROM " . $GLOBALS['os']->table('catalog') .
           " WHERE catalog_name LIKE '%".json_str_iconv($filters->keyword)."%' OR catalog_sn LIKE ".
           " '%".json_str_iconv($filters->keyword)."%' LIMIT 50";
    $arr = $db->getAll($sql);
    
    $opt = array();

    foreach ($arr AS $key => $val)
    {
        $opt[] = array('value' => $val['catalog_id'],
                       'text'  => $val['catalog_name'].'-'.$val['catalog_sn'],
                       'data'  => '');
    }

    make_json_result($opt);
}elseif($_REQUEST['act'] == 'add_catalog'){
	
	check_authz_json('send_catalog');
		
    include_once(ROOT_PATH . 'includes/cls_json.php');
    
    $json = new JSON;
    $fittings   = $json->decode($_GET['add_ids']);	
    $arr = $db->getAll("SELECT catalog_id,catalog_name FROM ".$os->table('catalog')." WHERE ".db_create_in($fittings,'catalog_id'));
    
    $opt = array();

    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['catalog_id'],
                       'text'       => $val['catalog_name'],
                       'data'       => '');
    }

    clear_cache_files();
    make_json_result($opt);    
}elseif($_REQUEST['act'] == 'list'){
	admin_priv('veiw_roster_catalog');		
    $smarty->assign('ur_here',      '派发目录明细列表');
    $smarty->assign('full_page',    1);
    
	if($_GET['user_type']==0){
		$sql = "SELECT cs.send_id,cs.send_status,cs.operate_time,c.catalog_name,r.consignee FROM ".$os->table('catalog_send')." cs LEFT JOIN ".
			   $os->table('catalog')." c ON cs.catalog_id = c.catalog_id LEFT JOIN ".$os->table('roster')." r ON cs.user_id=r.roster_id ".
			   " WHERE cs.user_type=0 AND cs.user_id=".intval($_GET['user_id']);
	}elseif($_GET['user_type']==1){
		
	}
	$send_cataloglist = $db->getAll($sql);
	$smarty->assign('send_cataloglist',$send_cataloglist);

    assign_query_info();
    
    $smarty->display('catalog_sendlist.tpl');	
}

/**
 *  返回用户列表数据
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_userlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 过滤条件 */
        $filter['keywords'] = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keywords'] = json_str_iconv($filter['keywords']);
        }

        $filter['sort_by']    = empty($_REQUEST['sort_by'])    ? 'user_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC'     : trim($_REQUEST['sort_order']);

        $ex_where = ' WHERE 1 ';
        if ($filter['keywords'])
        {
            $ex_where .= " AND user_name LIKE '%" . mysql_like_quote($filter['keywords']) ."%'";
        }     

        $filter['record_count'] = $GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['os']->table('users') . $ex_where);

        /* 分页大小 */
        $filter = page_and_size($filter);
        $sql = "SELECT user_id, user_name, email, sex, office_phone, home_phone, mobile_phone, reg_time ".
                " FROM " . $GLOBALS['os']->table('users') . $ex_where .
                " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
                " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        $filter['keywords'] = stripslashes($filter['keywords']);
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $user_list = $GLOBALS['db']->getAll($sql);

    $count = count($user_list);
    for ($i=0; $i<$count; $i++)
    {
 		$sex_list   =  array( 0 =>'保密', 1 => '男', 2 => '女');
    	$user_list[$i]['sex_format'] = in_array(intval($user_list[$i]['sex']), array(0, 1, 2)) ? $sex_list[$user_list[$i]['sex']] : 0;   
        $user_list[$i]['reg_time'] = local_date($GLOBALS['_CFG']['date_format'], $user_list[$i]['reg_time']);
    }

    $arr = array('user_list' => $user_list, 'filter' => $filter,
        'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}
?>